# -*- coding: latin-1 -*-
'''
Created on 21/09/2011

@author: nmedina
'''

import sys
import os
import wx
import wx.lib.platebtn as platebtn
from wx import xrc
import wx.lib.agw.gradientbutton as GB
import wx.lib.mixins.listctrl  as  listmix
import wx.richtext as rt
import wx.aui
import  wx.lib.masked as  masked  
import xml.dom.minidom
import  wx.lib.popupctl as pop
import  wx.calendar as  cal  
try:
    from agw import balloontip as BT
except ImportError: 
    import wx.lib.agw.balloontip as BT
    
try:
    from agw import labelbook as LB
    from agw.fmresources import *
except ImportError: 
    import wx.lib.agw.labelbook as LB
    from wx.lib.agw.fmresources import *
    
from ObjectListView import ObjectListView, ColumnDefn
import ObjectListView

try:
    from agw import foldpanelbar as fpb
except ImportError: # if it's not there locally, try the wxPython lib.
    import wx.lib.agw.foldpanelbar as fpb
    
import  wx.lib.stattext as ST

import controles.lista
class GenStaticTextXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "GenStaticText")

    def DoCreateResource(self):
        t = ST.GenStaticText(
          self.GetParentAsWindow()
          ,self.GetID()
          ,self.GetText("label")
          ,self.GetPosition()
          ,self.GetSize()
          ,platebtn.PB_STYLE_GRADIENT |platebtn.PB_STYLE_NOBG 
          ,self.GetName())
        self.SetupWindow(t)     
        return t



class PlateButtonXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "platebtn.PlateButton")

    def DoCreateResource(self):
        t = platebtn.PlateButton(
          self.GetParentAsWindow()
          , id_ = self.GetID()
          , name = self.GetName()
          , label = self.GetText("label")
          , pos = self.GetPosition()
          , size = self.GetSize()
          , style = platebtn.PB_STYLE_GRADIENT |platebtn.PB_STYLE_NOBG
        )
        t.SetPressColor(wx.Color(0,51,102))
        self.SetupWindow(t)     
        return t
    #style = platebtn.PB_STYLE_GRADIENT |platebtn.PB_STYLE_NOBG |platebtn.PB_STYLE_SQUARE
    
class PopupCalendarXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "pop.PopupCalendar")

    def DoCreateResource(self):
        self.t = pop.PopupControl(self.GetParentAsWindow()
                             ,self.GetID()
                             ,self.GetPosition())
        
        self.win = wx.Window(self.t,-1,pos = (0,0),style = 0)
        self.cal = cal.CalendarCtrl(self.win,-1,pos = (0,0))
        bz = self.cal.GetBestSize()
        self.win.SetSize(bz)
        self.t.SetPopupContent(self.win)  
        self.cal.Bind(cal.EVT_CALENDAR_DAY, self.OnCalSelected) 
        self.SetupWindow(self.t)  
        return self.t
        


    # Method called when a day is selected in the calendar
    def OnCalSelected(self,evt):
        self.t.PopDown()
        date = self.cal.GetDate()

        # Format the date that was selected for the text part of the control
        self.t.SetValue('%02d/%02d/%04d' % (date.GetDay(),
                                          date.GetMonth()+1,
                                          date.GetYear()))
        evt.Skip()
    def GetDate(self):
        return self.cal.GetDate()
    
    
class RichTextCtrlXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "rt.RichTextCtrl")

    def DoCreateResource(self):
        t = rt.RichTextCtrl(self.GetParentAsWindow(),
        id = self.GetID()
        , pos = self.GetPosition()
        , size = self.GetSize()
        ,style=wx.VSCROLL|wx.HSCROLL|wx.NO_BORDER | wx.TE_READONLY)
        t.SetBackgroundColour(self.GetText("bg"))
        return t
    
    
class SortListCtrlXmlHandler(xrc.XmlResourceHandler):

    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)

    def CanHandle(self, node):
        return self.IsOfClass(node, "Clases.SortListCtrl")



class GradientButtonXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "GB.GradientButton")

    def DoCreateResource(self):
        t = GB.GradientButton(
          self.GetParentAsWindow()
          , id = self.GetID()
          , name = self.GetName()
          , label = self.GetText("label")
          , pos = self.GetPosition()
          , size = self.GetSize()
          , style = wx.NO_BORDER | wx.PROCESS_ENTER
        )
        
        t.SetTopStartColour(wx.Colour(102,153,204))
        t.SetTopEndColour(wx.Colour(204,255,255))
        t.SetBottomStartColour(wx.Colour(204,255,255))
        t.SetBottomEndColour(wx.Colour(102,153,204))
        t.SetPressedTopColour('#3366FF')
        t.SetPressedBottomColour('#3399FF')
        t.SetForegroundColour('#000033')
       
        self.SetupWindow(t)     # handles font, bg/fg color
        return t
    
    
    

class AuiNotebookXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "aui.AuiNotebook")

    def DoCreateResource(self):
        t = wx.aui.AuiNotebook(
                                parent = self.GetParentAsWindow() 
                                , id= self.GetID()
                                , pos= self.GetPosition()
                                , size= self.GetSize()
##                                , style=wx.aui.AUI_NB_DEFAULT_STYLE
                                )
        
        self.SetupWindow(t)     # handles font, bg/fg color
        return t

    
class LabelBookXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "LB.LabelBook")

    def DoCreateResource(self):
        t = LB.LabelBook(
                          parent = self.GetParentAsWindow() 
                        , id= self.GetID()
                        , pos= self.GetPosition()
                        , size= self.GetSize()
                        , style=INB_LABEL_BOOK_DEFAULT
                                )
        
        self.SetupWindow(t)     # handles font, bg/fg color
        return t

class SashLayoutWindowXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "wxSashLayoutWindow")

    def DoCreateResource(self):
        t = wx.SashLayoutWindow(
          self.GetParentAsWindow()
          , id = self.GetID()
          , pos = self.GetPosition()
          , size = self.GetSize()
          #, style = self.GetStyle()
        )
        
       
       
        self.SetupWindow(t)     # handles font, bg/fg color
        return t
    
class wxSearchCtrlXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "wxSearchCtrl")

    def DoCreateResource(self):
        t = wx.SearchCtrl(
          self.GetParentAsWindow()
          , id = self.GetID()
          , pos = self.GetPosition()
          , size = self.GetSize()
          , style = wx.TE_PROCESS_ENTER )
          
        
       
        t.ShowCancelButton(True)
        
        self.SetupWindow(t)     # handles font, bg/fg color
        return t
    

    
    
    
class CuitCtrlXmlHandler(xrc.XmlResourceHandler):
    def __init__(self,style = wx.TE_PROCESS_TAB):
        self.style = style
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "masked.Cuil")

    def DoCreateResource(self):
        t = masked.TextCtrl( parent = self.GetParentAsWindow()
                            ,id = self.GetID()
                            , style = self.style
                            ,mask         = '##-########-#'
                            ,excludeChars = ""
                            ,formatcodes  = 'F'
                            ,includeChars = ""
                            ,validRegex   = "\d{2}-\d{8}-\d{1}"
                            ,validRange   =''
                            ,choices      = ''
                            ,choiceRequired = True
                            ,defaultValue = ''
                            ,demo         = True
                            ,name         = self.GetName())
          
        
       
#        t.SetBackgroundColour("#CCFFFF")
#        t.SetCtrlParameters(emptyBackgroundColour = '#CCFFFF' )
#        t.SetCtrlParameters(validBackgroundColour = '#CCFFFF' )
        
        self.SetupWindow(t)     # handles font, bg/fg color
        return t
    
    
class NroFacturaCtrlXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "masked.NroFactura")

    def DoCreateResource(self):
        t = masked.TextCtrl( self.GetParentAsWindow()
                            ,self.GetID()
                            , ""
                            ,mask         = '#{4}-#{8}'
                            ,excludeChars = ""
                            ,formatcodes  = 'F'
                            ,includeChars = ""
                            ,validRegex   = "\d{4}-(\s{8}|\d{8})"
                            ,validRange   =''
                            ,choices      = ''
                            ,choiceRequired = True
                            ,defaultValue = ''
                            ,demo         = True
                            ,name         = self.GetName())
          
        
       
#        t.SetBackgroundColour("#CCFFFF")
#        t.SetCtrlParameters(emptyBackgroundColour = '#CCFFFF' )
#        t.SetCtrlParameters(validBackgroundColour = '#CCFFFF' )
        self.SetupWindow(t)     # handles font, bg/fg color
        return t
    
class IpCtrlXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "masked.Ip")

    def DoCreateResource(self):
#        t = masked.IpAddrCtrl( self.GetParentAsWindow()
#                            ,self.GetID()
#                            ,mask=" #{3}.#{3}.#{3}.#{3}" 
#                            )
        t = masked.IpAddrCtrl( self.GetParentAsWindow(), self.GetID(), style = wx.TE_PROCESS_TAB )
        self.SetupWindow(t) 
            
        return t
    
class EmailCtrlXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "masked.Email")

    def DoCreateResource(self):
     
        t = masked.Ctrl( self.GetParentAsWindow()
                         , self.GetID()
                         , ""
                         ,autoformat       = "EMAIL"
                         ,demo             = True
                         ,name             = "EMAIL")
        self.SetupWindow(t) 
            
        return t
    
class MoneyCtrlXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "masked.Money")

    def DoCreateResource(self):
        t = masked.TextCtrl( self.GetParentAsWindow()
                            ,self.GetID()
                            , ""
                            ,mask         = '#{7}.#{2}'
                            ,excludeChars = ""
                            ,formatcodes  = 'F-_R'
                            ,includeChars = ""
                            ,validRegex   = ""
                            ,validRange   =''
                            ,choices      = ''
                            ,choiceRequired = True
                            ,defaultValue = '0000000.00'
                            ,demo         = True
                            ,name         = self.GetName())

        self.SetupWindow(t)    
        return t
    
    
    
    
class ToolBookXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "GB.ToolBook")

    def DoCreateResource(self):
        t = wx.Toolbook(self.GetParent()
                        ,self.GetID()
                        ,style= wx.BK_LEFT | wx.BK_HITTEST_ONLABEL
                        ,size = self.GetSize())

        t.SetBackgroundColour("81BEF7")
        self.SetupWindow(t)     # handles font, bg/fg color
        return t
    
    
    
class DatePickerCtrlXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "DatePickerCtrl")

    def DoCreateResource(self):
        t = wx.DatePickerCtrl(
                                parent = self.GetParentAsWindow() 
                                , id= self.GetID()
                                , pos= self.GetPosition()
                                , size= self.GetSize()
                                ,style= wx.DP_DROPDOWN
                                        | wx.DP_SHOWCENTURY
                                        #| wx.DP_ALLOWNONE
                                )
        
        self.SetupWindow(t)     # handles font, bg/fg color
        return t
    
    
class ListViewExtended(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "ListViewExtended")

    def DoCreateResource(self):
        t = controles.lista.ListViewExtended(
                                parent = self.GetParentAsWindow() 
                                , id= self.GetID()
                                , pos= self.GetPosition()
                                , size= self.GetSize()
                                ,style=wx.LC_REPORT|wx.LC_VIRTUAL|wx.LC_HRULES|wx.LC_VRULES|wx.FULL_REPAINT_ON_RESIZE
                                )
        
        self.SetupWindow(t)
        return t
    
    
class FoldPanelBarXmlHandler(xrc.XmlResourceHandler):
    def __init__(self):
        xrc.XmlResourceHandler.__init__(self)
        self.AddWindowStyles()

    def CanHandle(self, node):
        return self.IsOfClass(node, "FoldPanelBar")

    def DoCreateResource(self):

        t = fpb.FoldPanelBar(
                             self.GetParent()
                             , self.GetID()
                             , self.GetPosition()
                             ,self.GetSize()
                             , fpb.FPB_DEFAULT_STYLE )
        
        self.SetupWindow(t)
        return t
